package com.nhim.cms.util;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nhim.cms.dao.TemplateDAO;
import com.nhim.cms.domain.Template;
import com.nhim.core.AppContext;

@Service
public class TemplateManager {
	
	@Autowired
	private TemplateDAO templateDAO;
	
	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}
	@Transactional(readOnly = true)
	public List<Template> findContentTemplateById(Integer id) 
	{
		return templateDAO.findContentTemplateById(id);
	}
	@Transactional(readOnly = true)
	public List<Template> findAllSiteTemplate() 
	{
		return templateDAO.findAllSiteTemplate();
	}
	@Transactional(readOnly = true)
	public List<Template> findAllGroupTemplate() 
	{
		return templateDAO.findAllGroupTemplate();
	}
	@Transactional(readOnly = true)
	public Template findById(Integer id) {
		return templateDAO.findById(id);
	}
	@Transactional(readOnly = true)
	public Template findContentTemplateByName(Integer id,String name) {
		return templateDAO.findContentTemplateByName(id,name);
	}
	@Transactional(readOnly = true)
	public Template findGroupTemplateByName(String name) {
		return templateDAO.findGroupTemplateByName(name);
	}
	@Transactional(readOnly = true)
	public Template findSiteTemplateByName(String name) {
		
		return templateDAO.findSiteTemplateByName(name);
	}
	@Transactional(readOnly = true)
	public List<Template> findByProperty(String propertyName, final Object value) {
		return templateDAO.findByProperty(propertyName, value);
	}
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Template saveOrUpdate(Template entity)throws Exception {
		return templateDAO.saveOrUpdate(entity);
	}
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean remove(Integer Id)
	{
		Template entity = templateDAO.findById(Id);
		if(entity != null)
		{
			templateDAO.delete(entity);
			return true;
		}
		return false;
		
	}
	public static TemplateManager getInstance()
	{
		return TemplateManager.getFromApplicationContext(AppContext.getApplicationContext());
	}

	public static TemplateManager getFromApplicationContext(
			ApplicationContext ctx) {
		return (TemplateManager) ctx.getBean(TemplateManager.class);
	}
	

	

}
